home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / win2maccountersamples / 3. counterdialog / source / ccounterapp.cp next >
Encoding:
Text File  |  2000-09-28  |  4.4 KB  |  175 lines

  1. /*
  2.     File:        CCounterApp.cp
  3.  
  4.     Contains:    Sample code to accompany Chapter 12 of 
  5.                 "An Introduction to Macintosh Programming for Windows Programmers".
  6.                 
  7.     Written by:    Worldwide Developer Technical Support
  8.  
  9.     Copyright:    1999 Apple Computer, Inc., All Rights Reserved
  10.  
  11.       You may incorporate this sample code into your applications without
  12.       restriction, though the sample code has been provided "AS IS" and the
  13.       responsibility for its operation is 100% yours.  However, what you are
  14.       not permitted to do is to redistribute the source as "DSC Sample Code"
  15.       after having made changes. If you're going to re-distribute the source,
  16.      we require that you make it clear in the source that the code was
  17.     descended from Apple Sample Code, but that you've made changes.
  18.     
  19. */
  20.  
  21. #include "CCounterApp.h"
  22. #include "CounterConstants.h"
  23.  
  24. #include <LStaticText.h>
  25.  
  26. #include <LGrowZone.h>
  27. #include <LWindow.h>
  28. #include <PP_Messages.h>
  29. #include <PP_Resources.h>
  30. #include <PPobClasses.h>
  31. #include <UDrawingState.h>
  32. #include <UMemoryMgr.h>
  33. #include <URegistrar.h>
  34. #include <UModalDialogs.h>
  35.  
  36. #include <UControlRegistry.h>
  37. #include <UGraphicUtils.h>
  38. #include <UEnvironment.h>
  39.  
  40. #ifndef __APPEARANCE__
  41. #include <Appearance.h>
  42. #endif
  43.  
  44. #include <Sound.h>
  45. #include <ToolUtils.h>
  46.  
  47. // ===========================================================================
  48. int main()
  49. {
  50.     SetDebugThrow_(debugAction_Alert);
  51.     SetDebugSignal_(debugAction_Alert);
  52.     InitializeHeap(3);                    // allocate 3 Master Pointer blocks
  53.     UQDGlobals::InitializeToolbox(&qd);
  54.     UEnvironment::InitEnvironment();
  55.     new LGrowZone(20000);            // For low memory situations.
  56.     CCounterApp    theApp;            // stack allocation
  57.     theApp.Run();
  58.     return 0;
  59. }
  60.  
  61. // ---------------------------------------------------------------------------
  62. CCounterApp::CCounterApp()
  63. {
  64.     if ( UEnvironment::HasFeature( env_HasAppearance ) ) {
  65.         ::RegisterAppearanceClient();
  66.     }
  67.     RegisterAllPPClasses();    // functions to create core PowerPlant classes
  68.     UControlRegistry::RegisterClasses();    // Appearance Manager/GA classes
  69. }
  70.  
  71.  
  72. // ---------------------------------------------------------------------------
  73. CCounterApp::~CCounterApp()
  74. {
  75. }
  76.  
  77. // ---------------------------------------------------------------------------
  78. //    This function lets you do something when the application starts up
  79. //    without a document.
  80. void
  81. CCounterApp::StartUp()
  82. {
  83.     LWindow* theWindow;
  84.     theWindow = MakeControlsWindow();    
  85.     ThrowIfNil_(theWindow );
  86. }
  87.  
  88. // ---------------------------------------------------------------------------
  89. //    Respond to commands from menus, buttons, etc.
  90. Boolean
  91. CCounterApp::ObeyCommand(CommandT    inCommand, void* ioParam)
  92. {
  93.     Boolean    cmdHandled = true;
  94.     switch (inCommand) {
  95.         case cmd_SetValue:
  96.             Int32 newValue = 0;
  97.             if (AskForValue(newValue)) {
  98.                 fCounter.SetValue(newValue);
  99.                 fCaption->SetValue(fCounter.GetValue());
  100.             }
  101.             break;
  102.  
  103.         case cmd_Increment:
  104. //            ::SysBeep(1);
  105.             fCounter.Increment();
  106.             fCaption->SetValue(fCounter.GetValue());
  107.             break;
  108.  
  109.         case cmd_Decrement:
  110.             fCounter.Decrement();
  111.             fCaption->SetValue(fCounter.GetValue());
  112.             break;
  113.  
  114.         default:
  115.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  116.             break;
  117.     }
  118.     return cmdHandled;
  119. }
  120.  
  121. // ---------------------------------------------------------------------------
  122. //    This function enables menu commands.
  123. void
  124. CCounterApp::FindCommandStatus(
  125.     CommandT    inCommand,
  126.     Boolean        &outEnabled,
  127.     Boolean        &outUsesMark,
  128.     Char16        &outMark,
  129.     Str255        outName)
  130. {
  131.     switch (inCommand) {
  132.         // Return menu item status according to command messages.
  133.  
  134.         case cmd_SetValue:
  135.             outEnabled = true;            // enable the button
  136.             break;
  137.  
  138.         case cmd_Increment:
  139.             outEnabled = true;            // enable the button
  140.             break;
  141.  
  142.         case cmd_Decrement:
  143.             outEnabled = true;            // enable the button
  144.             break;
  145.  
  146.         default:
  147.             LApplication::FindCommandStatus(inCommand, outEnabled,
  148.                                                 outUsesMark, outMark, outName);
  149.             break;
  150.     }
  151. }
  152.  
  153. // ---------------------------------------------------------------------------
  154. LWindow*
  155. CCounterApp::MakeControlsWindow()
  156. {
  157.     LWindow* theWindow = LWindow::CreateWindow(rWindow_Sample, this );
  158.     
  159.     fCaption = dynamic_cast<LCaption*>(theWindow->FindPaneByID(kCaption));
  160.     ThrowIfNil_(fCaption);
  161.         
  162.     theWindow->Show();
  163.     return theWindow;
  164. }
  165.  
  166. // ---------------------------------------------------------------------------
  167. Boolean
  168. CCounterApp::AskForValue(Int32& newValue)
  169. {
  170.     Str255 numStr;
  171.     Boolean theResult = UModalDialogs::AskForOneNumber( this,
  172.                             rDialog_Value, kEditField, newValue );
  173.     return theResult;
  174. }
  175.